var server = require('api');
var _alert = require('alert');
var _clear = require('clearForm');

var org = avalon.define({
	$id : 'org',

    //队伍操作
    team: {
	    name: '',
        addr: '',
        data: [],
        newName: '',
        newAddr: '',
        newNote: '',
        newPhone: '',
        newType: '',

        modName: '',
        modAddr: '',
        modNote: '',
        modPhone: '',
        modType: '',

        rowData: '',
        groupInfo: ''
    },

    //modal
    delTeamModal: function () {

	    if (org.team.groupInfo){
	        $('#delTeamModal').modal('show');
        } else{
	        _alert('请先选择队伍')
        }
    },

    modTeamModal: function () {

        if (org.team.groupInfo){
            $('#modTeamModal').modal('show');

            var info = org.team.groupInfo;
            org.team.modName = info.groupName;
            org.team.modAddr = info.groupAddress;
            org.team.modNote = info.groupDES;
            org.team.modPhone = info.groupPhone;
        } else{
            _alert('请先选择队伍')
        }
    },


    //删除队伍
    delTeam: function () {
        // var info = $('#teamTable').bootstrapTable('getSelections');
        var groupId = org.team.groupInfo.groupId;
        var data = {userId: avalon.vmodels.root.user.id,userToken: avalon.vmodels.root.user.token,groupId:groupId};

        $.ajax({
            url:server.delTeam + 'userId=' + data.userId + '&userToken=' + data.userToken + '&groupId=' + data.groupId,
            type:'delete',
            dataType:'json',
        })
            .done(function (res) {
                if(res.status == '9200'){
                    $('#delTeamModal').modal('hide');
                    _alert('删除队伍成功');
                    queryTeam1();
                }else{
                    _alert('删除失败' + res.description)
                }

            })
            .fail(function(res) {
                $('#teamTable').bootstrapTable();
                _alert('网络错误，请刷新尝试！');
                avalon.log(JSON.parse(res) + _.now())
            })
    },


    //条件查询
    queryTeam: function () {
        $.ajax({
            url: server.queryTeam,
            type: 'get',
            dataType: 'json',
            data: {
                userId: avalon.vmodels.root.user.id,
                userToken:avalon.vmodels.root.user.token,
                groupName:org.team.name,
                groupAddress:org.team.addr
            }
        })
            .done(function(res) {
                $('#lodaingModal').modal('hide')
                var data = res.values;
                if(res.status == '9200'){
                    $('#teamTable').bootstrapTable('load',data);
                }else{
                    $('#teamTable').bootstrapTable('removeAll');
                    _alert('没有查到结果')
                }
            })
            .fail(function(res) {
                $('#teamTable').bootstrapTable();
                _alert('网络错误，请刷新尝试！');
                avalon.log(JSON.parse(res) + _.now())
            })
    },


	//添加队伍
    addTeam: function () {
	    var data = {
            userId: avalon.vmodels.root.user.id,
            userToken: avalon.vmodels.root.user.token,
            groupName: org.team.newName,
            groupDES: org.team.newNote,
            groupAddress: org.team.newAddr,
            groupPhone: org.team.newPhone
            // groupType: org.team.newType
        }

        $.ajax({
            url:server.addTeam,
            type:'post',
            dataType:'json',
            headers: {
                'Content-Type': 'application/json'
            },
            data:JSON.stringify(data)
        })
            .done(function (res) {
                if(res.status == '9200'){
                    _alert('添加队伍成功');
                    _clear('#addTeamModal');
                    $('#addTeamModal').modal('hide');
                    queryTeam1();
                }else{
                    _alert(res.description)
                }
            })
            .fail(function () {
                _alert('网络出错，添加队伍失败');
            })
    },


	//修改队伍
    modTeam: function () {

        // var info = $('#teamTable').bootstrapTable('getSelections');
        var groupId = org.team.groupInfo.groupId;
        var data = {
            userId: avalon.vmodels.root.user.id,
            userToken: avalon.vmodels.root.user.token,
            groupId: groupId,
            groupName: org.team.modName,
            groupDES: org.team.modNote,
            groupAddress: org.team.modAddr,
            groupPhone: org.team.modPhone
            // groupType: org.team.newType
        }

        console.log(org.team.groupInfo.$model)

        $.ajax({
            url:server.modTeam,
            type:'put',
            dataType:'json',
            headers: {
                'Content-Type': 'application/json'
            },
            data:JSON.stringify(data)
        })
            .done(function (res) {
                if(res.status == '9200'){
                    _clear('#modTeamModal');
                    $('#modTeamModal').modal('hide');
                    _alert('修改队伍信息成功');
                    queryTeam1();
                }else{
                    _alert('修改队伍信息失败 ' + res.description)
                }
            })
            .fail(function () {
                _alert('网络出错，修改队伍信息失败');
            })
    },

	init: function(){
		avalon.log("组织机构 init"+_.now());

		setTimeout(function () {

            $(function () {
                queryTeam1();
            })

        },100)
	}
})




//查询队伍信息数据
function queryTeam1() {

    org.team.groupInfo = '';

    $.ajax({
        url: server.queryTeam,
        type: 'get',
        dataType: 'json',
        data: {userId: avalon.vmodels.root.user.id,userToken:avalon.vmodels.root.user.token}
    })
        .done(function(res) {
            $('#lodaingModal').modal('hide')
            var data = res.values;
            if(res.status == '9200'){
                org.team.rowData = avalon.mix(true, [], data);
                $('#teamTable').bootstrapTable({});

                $('#teamTable').on('check.bs.table', function (e, row,ele) {
                    org.team.groupInfo = row;
                })

                //异步修改表格数据
                setTimeout(function () {
                    $('#teamTable').bootstrapTable('load', org.team.rowData.$model);

                    $('#teamTable').bootstrapTable('selectPage', 1);
                },10)
            }else{
                $('#teamTable').bootstrapTable({});
                _alert(res.description);
            }
        })
        .fail(function(res) {
            $('#lodaingModal').modal('hide')
            // $('#table').bootstrapTable();
            _alert('网络错误，请刷新尝试！');
        })
}

module.exports = org;